package api

import (
	"app/serializer"
	"app/service"
	"net/http"

	"github.com/gin-gonic/gin"
)

// CreateTask @Tags TASK
// @Summary 创建任务
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Router /task [post]
func CreateTask(c *gin.Context) {
	createService := service.CreateTaskService{}
	if err := c.ShouldBind(&createService); err == nil {
		uid, _ := c.Get("uid")
		res := createService.Create(uid.(uint))
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusBadRequest, serializer.ErrorResponse(err))
	}
}

// ListTasks @Tags TASK
// @Summary 获取任务列表
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Router /tasks [get]
func ListTasks(c *gin.Context) {
	listService := service.ListTasksService{}
	if err := c.ShouldBind(&listService); err == nil {
		uid, _ := c.Get("uid")
		res := listService.List(uid.(uint))
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusBadRequest, serializer.ErrorResponse(err))
	}
}

// ShowTask @Tags TASK
// @Summary 展示任务详细信息
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Router /task/:id [get]
func ShowTask(c *gin.Context) {
	showTaskService := service.ShowTaskService{}
	res := showTaskService.Show(c.Param("id"))
	c.JSON(http.StatusOK, res)
}

// DeleteTask @Tags TASK
// @Summary 删除任务
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Router /task/:id [delete]
func DeleteTask(c *gin.Context) {
	deleteTaskService := service.DeleteTaskService{}
	res := deleteTaskService.Delete(c.Param("id"))
	c.JSON(http.StatusOK, res)
}

// UpdateTask @Tags TASK
// @Summary 修改任务
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Router /task [put]
func UpdateTask(c *gin.Context) {
	updateTaskService := service.UpdateTaskService{}
	if err := c.ShouldBind(&updateTaskService); err == nil {
		res := updateTaskService.Update(c.Param("id"))
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusBadRequest, serializer.ErrorResponse(err))
	}
}

// SearchTasks @Tags TASK
// @Summary 查询任务
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Router /search [post]
func SearchTasks(c *gin.Context) {
	searchTaskService := service.SearchTaskService{}
	if err := c.ShouldBind(&searchTaskService); err == nil {
		uid, _ := c.Get("uid")
		res := searchTaskService.Search(uid.(uint))
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusBadRequest, serializer.ErrorResponse(err))
	}
}
